package com.neusoft.zqygbc.neusoft_wenda.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.activity.QuestionContentActivity;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.AnswersEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.PhotosEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Question;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.neusoft.zqygbc.neusoft_wenda.model.QusetionModel;
import com.neusoft.zqygbc.neusoft_wenda.view.NineGridTestLayout;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;


/**
 * Created by Lenovo on 2017/3/6.
 */

public class  QListAdapter extends XRecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<QuestionEntity> mDataList;
    private LayoutInflater mLayoutInflater;
    private GridAdapter mGridAdapter;
    private QusetionModel questionModel;
    private Call call;
    private RetrofitCallback<Question> questionCallback;

    public QListAdapter(Context mContext, ArrayList<QuestionEntity> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_home, parent, false);
//        LinearLayout.LayoutParams lp = new LinearLayout.
//                LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        v.setLayoutParams(lp);
        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final QuestionEntity entity = mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
     //   Picasso.with(mContext).load("http://115.159.93.244/wenda/Public/userphoto/" + entity.getPhotos()).into(viewHolder.home_userphoto);
        viewHolder.home_username.setText(entity.getUsername());
        viewHolder.home_date.setText(entity.getCreatetime());
        switch (entity.getTid()) {

            case "1":
                viewHolder.home_type.setText("Android");
                break;

            case "2":
                viewHolder.home_type.setText("PHP");
                break;

            case "3":
                viewHolder.home_type.setText("Web");
                break;

            case "4":
                viewHolder.home_type.setText("iOS");
                break;

            default:
                break;
        }
        viewHolder.home_description.setText(entity.getDescription());
        viewHolder.home_hittime_count.setText(entity.getHittime());
//        viewHolder.home_comment_count.setText();
        viewHolder.home_thumbup_count.setText(entity.getThumbup());

            List<String> list = new ArrayList<String>();
            for (int i = 0; i < entity.getPhotos().size(); i++)
                list.add("http://115.159.93.244/wenda/Public/questionimg/" + entity.getPhotos().get(i).getFilename());
            viewHolder.home_nine_grid.setIsShowAll(false); //当传入的图片数超过9张时，是否全部显示
            viewHolder.home_nine_grid.setSpacing(24); //动态设置图片之间的间隔
            viewHolder.home_nine_grid.setUrlList(list);

        viewHolder.qlist_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, QuestionContentActivity.class);
                intent.putExtra("qid", entity.getQid());
                intent.putExtra("question_uid", entity.getUid());
                mContext.startActivity(intent);
            }
        });

        questionModel = new QusetionModel();
        call = questionModel.getQuestionDetail(entity.getQid());
        questionCallback = new RetrofitCallback<Question>() {
            @Override
            public void onResponse(Question question) {
                List<AnswersEntity> answers = question.getQuestionDetail().getAnswers();
                viewHolder.home_answer_count.setText(String.valueOf(answers.size()));
            }
        };
        questionModel.bindCallback(call, questionCallback);

    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView home_userphoto;
        TextView home_username, home_date, home_type, home_description, home_hittime_count,
                home_answer_count, home_thumbup_count;
        LinearLayout qlist_container;
        NineGridTestLayout home_nine_grid;


        public ViewHolder(View itemView) {
            super(itemView);
            qlist_container = (LinearLayout) itemView.findViewById(R.id.qlist_container);
            home_userphoto = (ImageView) itemView.findViewById(R.id.home_userphoto);
            home_username = (TextView) itemView.findViewById(R.id.home_username);
            home_date = (TextView) itemView.findViewById(R.id.home_date);
            home_type = (TextView) itemView.findViewById(R.id.home_type);
            home_description = (TextView) itemView.findViewById(R.id.home_description);
            home_nine_grid = (NineGridTestLayout) itemView.findViewById(R.id.home_nine_grid);
            home_hittime_count = (TextView) itemView.findViewById(R.id.home_hittime_count);
            home_answer_count = (TextView) itemView.findViewById(R.id.home_answer_count);
            home_thumbup_count = (TextView) itemView.findViewById(R.id.home_thumbup_count);

        }
    }

}
